﻿using EasyIot.Common;
using EasyIot.Model;
using EasyIot.Service;
using EasyIot.WebApp.Common;
using EasyIot.WebApp.Controllers;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;

namespace EasyIot.WebApp.Areas.SysSet.Controllers
{
    public class DeviceController : BaseController
    {  
        public override ActionResult Index(int? id)
        {
            base.Index(id);
            return View();
        }

        [HttpGet]
        public JsonResult List(DeviceModel model, PageInfo pageInfo)
        {
            var result = DeviceService.GetListByFilter(model, pageInfo);
            return Json(result);
        }

        public ActionResult Add() 
        { 
            return View();
        }

        [HttpPost]
        public ActionResult Add(DeviceModel model)
        {
            if (DeviceService.IsExistCode(model.DeviceCode))
            {
                return Json(ErrorTip("该编码已经存在!"));
            }

            model.CreateTime = DateTime.Now;
            model.CreateUserId = Operator.UserId;
            model.UpdateTime = DateTime.Now;
            model.UpdateUserId = Operator.UserId;
            var result = DeviceService.Insert(model) ? SuccessTip("添加成功") : ErrorTip("添加失败");
            return Json(result);
        }

        public ActionResult Edit(int id)
        {
            var model = DeviceService.GetById(id);
            model.DeviceCode_EditBefore = model.DeviceCode;
            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(DeviceModel model)
        {
            if (!model.DeviceCode_EditBefore.Trim().ToLower().Equals(model.DeviceCode.Trim().ToLower()) && DeviceService.IsExistCode(model.DeviceCode))
            {
                return Json(ErrorTip("该编码已经存在!"));
            }

            model.UpdateTime = DateTime.Now;
            model.UpdateUserId = Operator.UserId;
            var result = DeviceService.UpdateById(model) ? SuccessTip("修改成功") : ErrorTip("修改失败");
            return Json(result);
        }

        [HttpGet]
        public JsonResult Delete(int id)
        {
            var result = DeviceService.DeleteById(id) ? SuccessTip("删除成功") : ErrorTip("删除失败");
            return Json(result);
        }

        [HttpGet]
        public JsonResult BatchDel(string idsStr)
        {
            var idsArray = idsStr.Substring(0, idsStr.Length - 1).Split(',');
            var result = DeviceService.DeleteByIds(idsArray) ? SuccessTip("批量删除成功") : ErrorTip("批量删除失败");
            return Json(result);
        }

        [HttpGet]
        public JsonResult SyncInfo(string devicecode)
        {
            string url = DataMgr.ApiUrl + "Control";
            
            string uid = DESEncrypt.Encrypt(devicecode);
            string data = "7002#" + devicecode + "#1##"; string content = DESEncrypt.Encrypt(data);
            string strJson = "{\"uid\":\"" + uid + "\",\"content\":\"" + content + "\"}";
            
            var jsonBack = HttpMethods.Post(url, strJson);

            int code = 200;
            JObject jsonInfo = (JObject)JsonConvert.DeserializeObject(jsonBack);
            if (jsonBack.Contains("code"))
            {
                code = jsonInfo.Value<int>("code");
            }

            var result = 200 == code ? SuccessTip("同步成功") : ErrorTip("同步失败");

            return Json(result);
        } 
    }
}